﻿using System;
using System.Collections.Generic;
using Web.UI.AdminLTE.Common;
using Web.UI.AdminLTE.Enums;
using Web.UI.TypeExtensions;

namespace Web.UI.AdminLTE.Controls.Simple
{
    public class ProgressBar : HtmlElementBase
    {
        private double _value;
        private double _min;
        private double _max = 100;
        private string _message;
        private int _decimals;
        private bool _showPercentage;
        private bool _active;
        private bool _striped;
        private ProgressBarType _progressBarType;

        public ProgressBar(double value)
        {
            _value = value;
            EnsureClass("progress-bar");
        }

        public ProgressBar Striped(bool value = true)
        {
            _striped = value;
            return this;
        }

        public ProgressBar Type(ProgressBarType progressBarType)
        {
            _progressBarType = progressBarType;
            return this;
        }

        public ProgressBar Value(double value)
        {
            _value = value;
            return this;
        }

        public ProgressBar Min(double min)
        {
            _min = min;
            return this;
        }

        public ProgressBar Max(double max)
        {
            _max = max;
            return this;
        }

        public ProgressBar Max(string message)
        {
            _message = message;
            return this;
        }

        public ProgressBar Round(int decimals)
        {
            _decimals = decimals;
            return this;
        }

        public ProgressBar ShowPercentage(bool value = true)
        {
            _showPercentage = value;
            return this;
        }

        public ProgressBar Active(bool value = true)
        {
            _active = value;
            return this;
        }

        public override string ToHtmlString()
        {
            if (_min > _max)
            {
                throw new ArgumentException("Min value must be less than or equal Max value");
            }
            var percentage = CalculatePercentage();
            var descr = string.Format("{0}% {1}", percentage, _message);
            return TagBuilderExtensions.New("div")
                .MergeHtmlAttributes(HtmlAttr.FormatHtmlAttributes())
                .AddOrMergeAttribute("role", "progressbar")
                .AddOrMergeAttribute("aria-valuenow", Convert.ToString(_value))
                .AddOrMergeAttribute("aria-valuemin", Convert.ToString(_min))
                .AddOrMergeAttribute("aria-valuemax", Convert.ToString(_max))
                .AddOrMergeAttribute("style", string.Format("width: {0}%", percentage))
                .AddOrMergeCssClass(_progressBarType != ProgressBarType.NotSet, _progressBarType.GetEnumDescription())
                .AddOrMergeCssClass(_active, "active")
                .AddOrMergeCssClass(_striped, "progress-bar-striped")
                .Html(!_showPercentage ? string.Format("<span class=\"sr-only\">{0}</span>", descr) : descr)
                .ToString();
        }

        private double CalculatePercentage()
        {
            return Math.Round((_value - _min) / (_max - _min) * 100, _decimals);
        }

        //  Common methods for all html elements
        #region Common Methods
        public ProgressBar HtmlAttributes(IDictionary<string, object> htmlAttributes)
        {
            MergeHtmlAttributes(htmlAttributes);
            return this;
        }

        public ProgressBar HtmlAttributes(object htmlAttributes)
        {
            MergeHtmlAttributes(htmlAttributes);
            return this;
        }

        public ProgressBar Id(string id)
        {
            SetId(id);
            return this;
        }

        public ProgressBar Class(string cssClass)
        {
            EnsureClass(cssClass);
            return this;
        }

        public ProgressBar Data(object htmlDataAttributes)
        {
            MergeHtmlDataAttributes(htmlDataAttributes);
            return this;
        }
        #endregion
    }
}